package com.xyz.gulimall.product.service.impl;

import com.xyz.common.utils.R;
import com.xyz.gulimall.product.dao.BrandDao;
import com.xyz.gulimall.product.dao.CategoryDao;
import com.xyz.gulimall.product.entity.BrandEntity;
import com.xyz.gulimall.product.entity.CategoryEntity;
import com.xyz.gulimall.product.vo.CategoryBrandVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.common.utils.PageUtils;
import com.xyz.common.utils.Query;

import com.xyz.gulimall.product.dao.CategoryBrandRelationDao;
import com.xyz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.xyz.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;

    @Resource
    private CategoryDao categoryDao;

    @Resource
    CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    CategoryBrandRelationDao categoryBrandRelationDao;


    /**
     * /product/categorybrandrelation/brands/list
     * @return
     */
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam("catId") Long catId){
        List<BrandEntity> categoryEntities = categoryBrandRelationService.fandBrandsList(catId);
        List<CategoryBrandVo> collect = categoryEntities.stream().map(item -> {
            CategoryBrandVo categoryBrandVo = new CategoryBrandVo();
            categoryBrandVo.setBrandId(item.getBrandId());
            categoryBrandVo.setBrandName(item.getName());
            return categoryBrandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }


    /**
     * /product/categorybrandrelation/brands/list
     * @param params
     * @return
     */
     @Override
     public PageUtils queryPage(Map<String, Object> params) {
              IPage<CategoryBrandRelationEntity> page = this.page(
                      new Query<CategoryBrandRelationEntity>().getPage(params),
                      new QueryWrapper<CategoryBrandRelationEntity>()
              );
              return new PageUtils(page);
     }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //查询详细名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);

    }

    @Override
    public List<BrandEntity> fandBrandsList(Long catId) {

        List<CategoryBrandRelationEntity> entities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().in("catelog_id", catId));
        List<BrandEntity> collect = entities.stream().map(item -> {
            Long brand = item.getBrandId();
            BrandEntity brandEntity = brandDao.selectById(brand);
            return brandEntity;
        }).collect(Collectors.toList());

        return collect;
    }


}